package com.astrocsr.action.system;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.astrocsr.action.BaseAction;
import com.astrocsr.business.SystemManager;
import com.astrocsr.common.Constants;
import com.astrocsr.common.helper.ContextHelper;
import com.astrocsr.domain.model.ParamSettingModel;
import com.astrocsr.domain.web.ParametersInfo;
import com.opensymphony.xwork2.ModelDriven;

/**
 * 
 * Created Oct 28, 2013
 */
@Controller("paramSettingAction")
@Scope("prototype")
public class ParamSettingAction extends BaseAction implements
		ModelDriven<ParamSettingModel> {

	private static final long serialVersionUID = 1L;
	private ParamSettingModel model = new ParamSettingModel();

	
	private @Autowired
	SystemManager systemManager;

	public ParamSettingModel getModel() {
		return model;
	}
	
	static final Logger log = Logger.getLogger(ParamSettingAction.class);

	public String getParamSetting() throws Exception {
		log.info("getParamSetting.start");
		try {
			// get ParametersInfo from business layer
			ParametersInfo parametersInfo1 = systemManager.getParameters(Constants.PARAM_NAME_TECH, Constants.PARAM_PASSWORD_REG_EXPR);
			model.setParametersInfo1(parametersInfo1);
			
			// get Total PWD history
			ParametersInfo parametersInfo2 = systemManager.getParameters(Constants.PARAM_NAME_TECH, Constants.PARAM_TOTAL_PASSWORD_HIST);
			if (parametersInfo2 == null) {
				parametersInfo2 = new ParametersInfo(Constants.PARAM_TOTAL_PASSWORD_HIST_DEFAULT + "");
			}
			model.setParametersInfo2(parametersInfo2);
			
			// get MaxBadAttemptsParam
			ParametersInfo parametersInfo3 = systemManager.getParameters(Constants.PARAM_NAME_TECH, Constants.PARAM_PWD_BAD_ATTEMP);
			if (parametersInfo3 == null) {
				parametersInfo3 = new ParametersInfo(Constants.PARAM_PWD_BAD_ATTEMP_DEFAULT + "");
			}
			model.setParametersInfo3(parametersInfo3);

			// get ExpiredDaysParam
			ParametersInfo parametersInfo4 = systemManager.getParameters(Constants.PARAM_NAME_TECH, Constants.PARAM_PWD_EXPIRED_DAYS);
			if (parametersInfo4 == null) {
				parametersInfo4 = new ParametersInfo(Constants.PARAM_PWD_EXPIRED_DAYS_DEFAULT + "");
			}
			model.setParametersInfo4(parametersInfo4);

		} catch (Exception ex) {
			log.error(ex.getMessage(), ex);
			throw ex;
		}
		log.info("getParamSetting.end");
		return SUCCESS;
	}
	
	public String createParameter() throws Exception {
		log.info("createParameter.start");
		try {

			ParametersInfo parametersInfo = model.getParametersInfo();
			if (parametersInfo != null  && StringUtils.isNotBlank(parametersInfo.getParamCode())
										&& StringUtils.isNotBlank(parametersInfo.getParamValue())) {
				
				parametersInfo.setMerName(Constants.MERCHANT);
				parametersInfo.setCreatedBy(ContextHelper.getLoginId());
				parametersInfo.setParamType(Constants.PARAM_NAME_TECH);
				parametersInfo.setParamName(parametersInfo.getParamCode());
				systemManager.updateParameter(parametersInfo);

			}
			// reload parameters
			getParamSetting();

		} catch (Exception ex) {
			log.error(ex.getMessage(), ex);
			throw ex;
		}
		log.info("createParameter.end");
		return SUCCESS;
	}
}